<template></template>

<script>
import Cookies from 'js-cookie';
import store from '@/store'
import { MessageBox } from 'element-ui'
export default {
    methods: {
        init() {
            try {
                if (typeof WebSocket === "undefined") {
                    alert("您的浏览器不支持socket");
                }
                if (typeof MozWebSocket === "function") {
                    WebSocket = MozWebSocket;
                }

                // if (name) {
                if (Cookies.get('Admin-Token')) {
                    let wsuri =
                        `${window.location.protocol == "https:" ? "wss" : "ws"}://${window.location.host
                        }/api/userLoginWebsocket/` + Cookies.get('username'); //ws地址
                    this.$quoWebsocket.update_websocket(new WebSocket(wsuri))
                    this.$quoWebsocket.quo_websocket.onopen = () => { }

                    this.$quoWebsocket.quo_websocket.onmessage = (res) => {
                        if (res.data == 'close') {
                            this.$quoWebsocket.quo_websocket.close();

                            MessageBox.confirm('您的账号已在其它地方登录，您可以继续留在该页面，或者重新登录', '系统提示', { confirmButtonText: '重新登录', cancelButtonText: '取消', type: 'warning' }).then(() => {
                                store.dispatch('LogOut').then(() => {
                                    location.href = '/index';
                                })
                            })
                        }
                    }
                    this.$quoWebsocket.quo_websocket.onclose = (res) => {
                        console.log(res, 'res')
                    }
                }



                // const wsuri = this.url +this.$store.state.user.name

                //
                // this.$quoWebsocket.quo_websocket = new WebSocket(url)
            } catch (err) {
                console.log(err);
            }

        }
    }

}
</script>

<style></style>